home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / SDL / examples / loopwave.c < prev    next >
C/C++ Source or Header  |  2002-10-27  |  3KB  |  131 lines

  1.  
  2. /* Program to load a wave file and loop playing it using SDL sound */
  3.  
  4. /* loopwaves.c is much more robust in handling WAVE files -- 
  5.     This is only for simple WAVEs
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11.  
  12. #include "SDL.h"
  13. #include "SDL_audio.h"
  14.  
  15. #ifdef __SASC
  16. #include <pragmas/SDL_pragmas.h>
  17. #include <proto/exec.h>
  18. #else
  19. #include <inline/SDL.h>
  20. #include <exec/types.h>
  21. #include <inline/exec.h>
  22. #endif
  23.  
  24. extern struct ExecBase *SysBase;
  25. struct Library *SDLBase=NULL;
  26.  
  27. static int done = 0;
  28.  
  29. void SDL_Quit(void)
  30. {
  31.     SDL_RealQuit();
  32.     CloseLibrary(SDLBase);
  33. }
  34.  
  35. struct {
  36.     SDL_AudioSpec spec;
  37.     Uint8   *sound;            /* Pointer to wave data */
  38.     Uint32   soundlen;        /* Length of wave data */
  39.     int      soundpos;        /* Current play position */
  40. } wave;
  41.  
  42. __saveds void fillerup(void *unused, Uint8 *stream, int len)
  43. {
  44.     Uint8 *waveptr;
  45.     int    waveleft;
  46.  
  47.     if( (wave.soundlen-wave.soundpos)<=0 )
  48.     {
  49.         done=1;
  50.  
  51.         
  52.         return;
  53.     }
  54.  
  55.     /* Set up the pointers */
  56.     waveptr = wave.sound + wave.soundpos;
  57.     waveleft = wave.soundlen - wave.soundpos;
  58.  
  59.     if ( waveleft <= len ) {
  60.         SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
  61.         wave.soundpos=wave.soundlen;
  62.         stream+=waveleft;
  63.  
  64.         memset(wave.sound,0,(len-waveleft));
  65.         SDL_MixAudio(stream,wave.sound,(len-waveleft),SDL_MIX_MAXVOLUME);
  66.         return;
  67.     }
  68.  
  69.     SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
  70.     wave.soundpos += len;
  71. }
  72.  
  73. void poked(int sig)
  74. {
  75.     done = 1;
  76. }
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80.     char name[32];
  81.  
  82.  
  83.     SDLBase=OpenLibrary("SDL.library",0L);
  84.  
  85.     if(!SDLBase) {
  86.         printf("Unable to open SDL.library\n");
  87.         exit(0);
  88.     }
  89.  
  90.     /* Load the SDL library */
  91.     if ( SDL_RealInit(SDL_INIT_AUDIO) < 0 ) {
  92.         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  93.         exit(1);
  94.     }
  95.     atexit(SDL_Quit);
  96.  
  97.     if ( argc!=2  ) {
  98.         printf( "Usage: %s <wavefile>\n", argv[0]);
  99.         exit(1);
  100.     }
  101.  
  102.     /* Load the wave file into memory */
  103.     if ( SDL_LoadWAV(argv[1],
  104.             &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
  105.         fprintf(stderr, "Couldn't load %s: %s\n",
  106.                         argv[1], SDL_GetError());
  107.         exit(1);
  108.     }
  109.     wave.spec.callback = fillerup;
  110.  
  111.  
  112.     /* Initialize fillerup() variables */
  113.     if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
  114.         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
  115.         SDL_FreeWAV(wave.sound);
  116.         exit(2);
  117.     }
  118.     SDL_PauseAudio(0);
  119.  
  120.     /* Let the audio run */
  121.     printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
  122.     while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) ) {
  123.         SDL_Delay(1000);
  124.     }
  125.  
  126.     /* Clean up on signal */
  127.     SDL_CloseAudio();
  128.     SDL_FreeWAV(wave.sound);
  129.     return(0);
  130. }
  131.